package com.etc.controller;

import com.etc.dto.CountVO;
import com.etc.dto.Result;
import com.etc.entity.*;
import com.etc.service.DoctorService;
import com.etc.service.OrderService;
import com.etc.util.FileUtil;
import com.etc.util.QiniuCloudUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@MultipartConfig
@Controller
@RequestMapping("/doctor")
public class DoctorController {

    private static final Logger logger = LoggerFactory.getLogger(DoctorController.class);

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderService orderService;

    @GetMapping("/phonenumber")
    public @ResponseBody
    Result existOfPhoneNumber(@RequestParam String phone) {
        Result result = new Result();
        boolean flag = doctorService.existOfPhone(phone);
        result.ok(flag);
        return result;
    }

    @PostMapping("/add")
    public @ResponseBody
    Result add(@RequestParam String doctorName, @RequestParam Integer deptId, @RequestParam String phone,
               @RequestParam String gender, String image, MultipartFile file) {
        Result result = new Result();

        //使用MultipartFile接口中的API方法对文件进行处理
        //判断是否有文件上传
        if (file != null) {
            //校验文件内容和大小
            if (!FileUtil.isImageFileAllowed(file.getContentType())) {
                result.fail(0, "文件类型不合法，只允许图片类型:" + FileUtil.IMAGR_FILE_CONTENT_TYPE);
                return result;
            } else if (file.getSize() > 10485760) {
                result.fail(0, "文件大小不合法，最大只支持10M");
                return result;
            } else {
                try {
                    //调用七牛工具类实现上传
                    String s = QiniuCloudUtil.uploadImageFile(file);
                    if (s != null) {
                        image = s;
                        logger.info("++++++++s:" + s);
                        result.ok(s);
                    } else {
                        result.fail(0, "七牛异常");
                    }
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        }

        boolean flag = doctorService.addDoctor(doctorName, deptId, phone, gender, image);
        if (flag) {
            result.ok(flag);
        } else {
            result.fail(0, "添加失败");
        }
        return result;
    }

    @PostMapping("/login")
    public @ResponseBody
    Result login(@RequestParam String phone, @RequestParam String password,HttpSession session) {
        Result result = new Result();
        Doctor doctor = doctorService.login(phone, password);
        if (doctor != null) {
            Boolean falg = redisTemplate.hasKey("doctorId");
            if (falg) {
                redisTemplate.delete("doctorId");
            }
            redisTemplate.opsForValue().set("doctorId", doctor.getDoctorId());
            session.setAttribute("user",doctor.getDoctorName());
            result.ok(doctor);
        } else {
            result.fail(0, "登录失败");
        }
        return result;
    }

    @GetMapping("/logout")
    public @ResponseBody
    Result logout() {
        doctorService.logout();
        return new Result().ok(true);
    }

    @GetMapping("/session")
    public @ResponseBody
    Result session() {
        Result result = new Result();
        Doctor doctorBySession = doctorService.getDoctorBySession();
        if (doctorBySession != null) {
            result.ok(doctorBySession);
        } else {
            result.fail(0, "session中无数据");
        }
        return result;
    }

    @GetMapping("/doctorlist")
    public @ResponseBody
    Result getDoctorList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, String condition) {
        Result result = new Result();
        Doctor doctor = new Doctor();
        PageInfo<Doctor> doctorListByPage = doctorService.getDoctorListByPage(doctor, pageNum, pageSize);

        if (!doctorListByPage.getList().isEmpty()) {
            result.ok(doctorListByPage);
        } else {
            result.fail(0, "没有相关数据");
        }
        return result;
    }

    @GetMapping("/getdoctorbyid")
    public @ResponseBody
    Result getDoctorById(@RequestParam String doctorId) {
        Result result = new Result();
        Doctor doctor = new Doctor();
        doctor.setDoctorId(doctorId);
        List<Doctor> doctorListSelective = doctorService.getDoctorListSelective(doctor);
        result.ok(doctorListSelective);
        return result;
    }

    @PostMapping("/update")
    public @ResponseBody
    Result update(@RequestParam String doctorId, @RequestParam String doctorName, @RequestParam String password, @RequestParam Integer deptId,
                  @RequestParam String phone, @RequestParam String gender, String image, MultipartFile file) {
        Result result = new Result();

        //使用MultipartFile接口中的API方法对文件进行处理
        //判断是否有文件上传
        if (file != null) {
            //校验文件内容和大小
            if (!FileUtil.isImageFileAllowed(file.getContentType())) {
                result.fail(0, "文件类型不合法，只允许图片类型:" + FileUtil.IMAGR_FILE_CONTENT_TYPE);
                return result;
            } else if (file.getSize() > 10485760) {
                result.fail(0, "文件大小不合法，最大只支持10M");
                return result;
            } else {
                try {
                    //调用七牛工具类实现上传
                    String s = QiniuCloudUtil.uploadImageFile(file);
                    if (s != null) {
                        logger.info("++++++++s:" + s);
                        image = s;
                        logger.info("++++++++imageAddress:" + s);
                    } else {
                        result.fail(0, "七牛异常");
                    }
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
        }

        boolean flag = doctorService.updateDoctor(doctorId, doctorName, password, deptId, phone, gender, image);
        if (flag) {
            result.ok(flag);
        } else {
            result.fail(0, "更新失败");
        }
        return result;
    }


    @GetMapping("/getPatientDetail")
    public @ResponseBody
    Result getPatientDetail(@RequestParam String patientId, @RequestParam String orderId) {
        Result result = new Result();
        List<Patient> patientDetail = doctorService.getPatientDetail(patientId);
        Order order = new Order();
        order.setOrderId(orderId);
        order.setStatus(3);
        orderService.updateOrderStatusByOrderId(order);
        if (!patientDetail.isEmpty()) {
            return result.ok(patientDetail.get(0));
        } else {
            return result.fail(0, "无该患者");
        }
    }

    @GetMapping("/getHistoryDetail")
    public @ResponseBody
    Result getHistoryDetail(@RequestParam String patientId) {
        Result result = new Result();
        List<History> historyDetail = doctorService.getHistoryDetail(patientId);
        if (!historyDetail.isEmpty()) {
            return result.ok(historyDetail);
        } else {
            return result.fail(0, "无该患者");
        }
    }

    @GetMapping("/getHistoryDetailByDoctorId")
    public @ResponseBody
    Result getHistoryDetailBydoctorId(@RequestParam String doctorId) {
        Result result = new Result();
        List<History> historyDetail = doctorService.getPatientAndHistoryByDoctorId(doctorId);
        if (!historyDetail.isEmpty()) {
            return result.ok(historyDetail);
        } else {
            return result.fail(0, "无该患者");
        }
    }

    @PostMapping("/getTime")
    public @ResponseBody
    Result getTime(@RequestParam String doctorId) {
        Result result = new Result();
        List<Time> times = doctorService.getTimeByDoctorId(doctorId);
        for (Time time : times) {
            String replace = time.getContent().replace("-", "/");
            time.setContent(replace);
        }
        if (!times.isEmpty()) {
            return result.ok(times);
        } else {
            return result.fail(0, "没有排班");
        }
    }

    @GetMapping("/count")
    public @ResponseBody
    Result count() {
        Result result = new Result();
        List<CountVO> countVOS = orderService.getCount();
        if (!countVOS.isEmpty()){
            return result.ok(countVOS);
        }
        return result.fail(0,"没有相关数据");
    }
}
